#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<stdlib.h>
typedef int QueueDataType;
typedef struct QueueData{
	QueueDataType Data;
	struct QueueData* Next;
}QD;
typedef struct{
	QD* head;
	QD* rear;
}Queue;
typedef struct{
	Queue queue1;
	Queue queue2;
} MyStack;
void QueueInit(Queue*tmp) {
	tmp->head = tmp->rear = NULL;
}
bool QueueEmpty(Queue* tmp) {
	assert(tmp);
	return tmp->rear == NULL;
}
MyStack* myStackCreate() {
	MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
	QueueInit(&stack->queue1);
	QueueInit(&stack->queue2);
	return stack;
}
void QueuePush(Queue*tmp,QueueDataType x) {
	assert(tmp);
	QD* p = (QD*)malloc(sizeof(QD));
	if (p == NULL) {
		perror("Push:malloc");
		return;
	}
	p->Data = x;
	p->Next = NULL;
	if (tmp->rear == NULL) {
		tmp->head = tmp->rear = p;
	}
	else {
		tmp->rear->Next = p;
		tmp->rear = p;
	}
}
QueueDataType QueuePop(Queue*tmp) {
	assert(tmp);
	QueueDataType data;
	if (QueueEmpty(tmp)) {
		perror("QueuePop:NULL");
	}
	else if (tmp->head == tmp->rear) {
		data = tmp->head->Data;
		free(tmp->head);
		tmp->head = tmp->rear = NULL;
	}
	else {
		data = tmp->head->Data;
		QD* p = tmp->head;
		tmp->head = tmp->head->Next;
		free(p);
		p = NULL;
	}
	return data;
}
void myStackPush(MyStack* obj, QueueDataType x) {
	if (QueueEmpty(&obj->queue2)) {
		QueuePush(&obj->queue1, x);
	}
	else {
		QueuePush(&obj->queue2, x);
	}
}

QueueDataType myStackPop(MyStack* obj) {
	if (QueueEmpty(&obj->queue1) && QueueEmpty(&obj->queue2)) {
		perror("StackPop:NULL");
	}
	if (QueueEmpty(&obj->queue2)) {
		while (obj->queue1.head != obj->queue1.rear) {
			QueuePush(&obj->queue2, QueuePop(&obj->queue1));
		}
		return QueuePop(&obj->queue1);
	}
	else {
		while (obj->queue2.head != obj->queue2.rear) {
			QueuePush(&obj->queue1, QueuePop(&obj->queue2));
		}
		return QueuePop(&obj->queue2);
	}
}

QueueDataType myStackTop(MyStack* obj) {
	if (QueueEmpty(&obj->queue1) && QueueEmpty(&obj->queue2)) {
		perror("StackTop:NULL");
		return 0;
	}
	if (QueueEmpty(&obj->queue2)) {
		return obj->queue1.rear->Data;
	}
	else {
		return obj->queue2.rear->Data;
	}
}

bool myStackEmpty(MyStack* obj) {
	return (QueueEmpty(&obj->queue1) && QueueEmpty(&obj->queue2));

}

void myStackFree(MyStack* obj) {
	QD* cur = obj->queue1.head;
	while (cur) {
		QD* tmp = cur;
		cur = cur->Next;
		free(tmp);
		tmp = NULL;
	}
	cur = obj->queue2.head;
	while (cur) {
		QD* tmp = cur;
		cur = cur->Next;
		free(tmp);
		tmp = NULL;
	}
}
int main() {

}